package com.mgzf.upload.service.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.mgzf.sdk.conf.configuration.SdkConfConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * Created by mike on 18/1/23.
 */
@Configuration
@Import(SdkConfConfiguration.class)
@MapperScan("com.mgzf.upload.service.dao.mapper")
public class UploadServiceConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        DataSource ds = dataSource();
        sqlSessionFactoryBean.setDataSource(ds);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/com/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }
}
